/*
 * Created by JFormDesigner on Sat Mar 03 18:59:31 GMT 2007
 */

package mv.releaser.view;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

import javax.swing.*;
import javax.swing.border.*;

import mv.releaser.controler.ControlerMvReleaser;
import mv.releaser.model.CRC;
import mv.releaser.model.MvReleaser;

import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * @author kal kal
 */
public class CrcWindow extends JFrame implements Observer {
	private ControlerMvReleaser	controler;
	private MvReleaser	mvReleaser;
	private String				size;

	public CrcWindow(ControlerMvReleaser controler,
			MvReleaser mvReleaser) {
		this.controler = controler;
		this.mvReleaser = mvReleaser;
		size = mvReleaser.getSize();
		initComponents();

		jProgressBar1.setMaximum(Integer.parseInt(size));

	}

	private void thisWindowClosing(WindowEvent e) {
		controler.crcProgressBarClosing();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - kal kal
		panel1 = new JPanel();
		jProgressBar1 = new JProgressBar();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setTitle("CRC");
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				thisWindowClosing(e);
			}
		});
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== panel1 ========
		{
			panel1.setBorder(new TitledBorder("CRC Calculation"));

			// JFormDesigner evaluation mark
//			panel1.setBorder(new javax.swing.border.CompoundBorder(
//				new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
//					"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
//					javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
//					java.awt.Color.red), panel1.getBorder())); panel1.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

			panel1.setLayout(new FormLayout(
				new ColumnSpec[] {
					FormFactory.UNRELATED_GAP_COLSPEC,
					FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
					new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
					FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
					new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
					FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
					FormFactory.UNRELATED_GAP_COLSPEC
				},
				new RowSpec[] {
					FormFactory.UNRELATED_GAP_ROWSPEC,
					FormFactory.LINE_GAP_ROWSPEC,
					new RowSpec(Sizes.dluY(15)),
					FormFactory.LINE_GAP_ROWSPEC,
					FormFactory.UNRELATED_GAP_ROWSPEC
				}));

			//---- jProgressBar1 ----
			jProgressBar1.setMaximum(1000);
			jProgressBar1.setStringPainted(true);
			panel1.add(jProgressBar1, cc.xywh(2, 3, 4, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
		}
		contentPane.add(panel1, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(null);
		// //GEN-END:initComponents
	}

	public void update(Observable arg0, Object arg1) {
		if (arg1 != null) {
			jProgressBar1.setValue((Integer) arg1);
		}
	}

	public long getCRC(File fin) throws IOException {
		FileInputStream f = new FileInputStream(fin);
		CheckedInputStream cis = new CheckedInputStream(f, new CRC32());
		
		// Computer CRC32 checksum
		byte[] buf = new byte[128];
		int value = 0;
		while (cis.read(buf) >= 0) {
			value += 128;
			jProgressBar1.setValue(value);
		}

		f.close();
		cis.close();

		return cis.getChecksum().getValue();
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - kal kal
	private JPanel panel1;
	private JProgressBar jProgressBar1;
	// JFormDesigner - End of variables declaration //GEN-END:variables
}
